﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Mvc;
using System.Windows;
using System.Windows.Controls;
using AutoMapper;
using FazaDemo.BL.SubDomains;
using FazaDemo.PL.Mapping;
using FazaDemo.PL.ViewFactories;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Client;
using Microsoft.Owin.Cors;
using Microsoft.Owin.Hosting;
using Newtonsoft.Json;
using Owin;
using System.Web.Http;
using FazaDemo.PL.AbstractPresenters;
using FazaDemo.PL.Wpf;
using FazaDemo.ViewModels;
using FazaDemo.BL.AppFacade;
using FazaDemo.BL.InterfaceDomain;

namespace FazaDemo.PL.Wpf
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {

        private HubConnection _hubCon;

        private IHubProxy _hubProxy;

        public static string SignalrAddress
        {
            get { return "http://172.19.195.10:8780/";}
        }

        public IEnumerable<Menu_Item> Items { get; set; }

        public Account AppAccount { get; set; }

        protected override void OnStartup(StartupEventArgs e)
        {
            //base.OnStartup(e);
            Mapper.CreateMap<char?, IEnumerable<SelectListItem>>().ConvertUsing<CharEnumSelectListItemsConverter>();
            Mapper.CreateMap<NavCondElement, NavElementViewModel>()
                .ForMember(m => m.NavDropItems, el => el.MapFrom(src => src.FieldDropType))
                .ForMember(m => m.SelectedItemId, el => el.Ignore())
                .ForMember(m => m.ElemEdit, el => el.Ignore());
            Mapper.CreateMap<NavigatorCondition, NavPageViewModel>()
                .ForMember(m => m.NavArea, c => c.MapFrom(src => src.CondElements))
                .ForMember(m => m.SelectedKindId, c => c.Ignore());
            Mapper.AssertConfigurationIsValid();
            IViewFactory factory = new WorkAreaFactory();
            IPresenter basePresenter = new BasePresenter(factory);
            IMenuReceiver receiver = new MenuReceiver();
            IEnumerable<Menu_Item> menuItems = receiver.GetMenuItems();
            IWorkAreaViewModel viewModel = new WorkAreaViewModel{Items = menuItems};
            IWorkAreaPresenter presenter = new WorkAreaPresenter(basePresenter, viewModel);
            Window workArea = presenter.Initialize();
            Current.MainWindow = workArea;
            presenter.PrepareMenu(workArea);
            workArea.Show();
        }

        private void StartSignalR()
        {
            WebApp.Start(SignalrAddress);
        }

        private void Connect()
        {
            _hubCon = new HubConnection(SignalrAddress);
            _hubProxy = _hubCon.CreateHubProxy("AppHub");
            try
            {
                _hubCon.Start();
            }
            catch (Exception)
            {
                throw;
            }
            _hubProxy.On<string, string>("addMessage", (invoker, data) =>
            {
                if (invoker == "loginButton")
                {
                    AppAccount = JsonConvert.DeserializeObject<Account>(data);
                    if (AppAccount.Role == "Администратор")
                    {
                        //BuildMenu(Items, Items,  P_MainMenu, new List<Menu_Item>());
                    }
                }
            }
            );
        }

    }
}
